package dev.marsh.hunter

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.text.TextUtils

/**
 * Global settings
 *
 * @author zhaozhe
 * Created on 2018/7/15.
 */

const val TAG = "[Hunter]"

/**
 * Check settings
 */
fun checkSettings(context: Context): Boolean {
    val pkg = context.packageName
    val flag = Settings.Secure.getString(context.contentResolver,
            "enabled_notification_listeners")
    if (!TextUtils.isEmpty(flag)) {
        val names = flag.split(":")
        for (name in names) {
            val componentName = ComponentName.unflattenFromString(name)
            componentName?.let {
                if (it.packageName == pkg) {
                    return true
                }
            }
        }
    }
    return false
}

/**
 * Go settings
 */
fun goSettings(context: Context) {
    val settingsIntent = Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")
    settingsIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
    context.startActivity(settingsIntent)
}
